<template>
	<div class="btn-group">
		<button @click="toChapter" class="btn btn-white btn-xs btn-info btn-round">
			大章
		</button>&nbsp;
		<button @click="showContent" class="btn btn-white btn-xs btn-info btn-round">
			内容
		</button>&nbsp;
		<button @click="edit" class="btn btn-white btn-xs btn-info btn-round">
			编辑
		</button>&nbsp;		
		<button @click="del" class="btn btn-white btn-xs btn-warning btn-round">
			删除
		</button>
	</div>
</template>

<script>
	export default {
		name: 'operation',
		props: {
			course: Object
		},
		methods: {
			toChapter() {
				this.$global.session.set('course', this.course)
				this.$router.push('/business/chapter')
			},
			edit() {
				this.$parent.course = $.extend({}, this.course)
				this.$parent.categoryIdList = this.course.categoryIdList
				$('#save-modal').modal('show')
			},
			del() {
				this.$confirm({
						title: ' ',
						content: '确定删除课程: ' + this.course.name + '?'
					})
					.then(() => {
						let url = process.env.VUE_APP_SERVER + 'business/admin/course/delete/' + this.course.id
						console.log(url)
						this.$axios.delete(url)
							.then(res => {
								if (res.data.status === 1) {
									this.$toast('已删除课程:' + this.course.name, this.$global.toast.success)
									this.$parent.list(1)
								} else {
									this.$toast(res.data.msg, this.$global.toast.fail)
								}
							})						
						// this.$emit('del', this.course)
					})
					.catch(() => {})
			},
			showContent() {
				let _this = this
				this.$parent.course = $.extend({}, this.course)
				console.log(this.$parent.course)
				let url = process.env.VUE_APP_SERVER + 'business/admin/course/getContent/' + this.course.id
				this.$axios.get(url)
					.then(res => {
						if (res.data.status === 1) {
							$('#summernote').summernote({height: 350, focus: true})
							$('#summernote').summernote('code', res.data.data ? res.data.data.content : '')
							$('#content-modal').modal('show')
							//每隔10s保存富文本的内容
							let saveContentTask = setInterval(function() {
								_this.$parent.$refs.contentModal.save()
							}, 30000)
							//模态框关闭时，结束定时任务
							$('#content-modal').on('hidden.bs.modal', function(e) {
								console.log('clearInterval')
								clearInterval(saveContentTask)
							})
						} else {
							this.$toast(res.data.msg, this.$global.toast.fail)
						}						
					})
			}
		}
	}
</script>

<style>
</style>
